/*
 * Created by: vadikv
 * Created: Monday, March 10, 2008
 */

using OneDayModel.Interfaces;
using OneDayModel.Services;

namespace OneDayModel.Services
{
    public class ConfirmationServices : IConfirmationServices
    {
        private readonly IRepository repository;
        private readonly IMailSender mailSender;
        private readonly IMailComposer mailComposer;
        private readonly IIdGenerator confirmationIdGenerator;

        public void SendConfirmation(string alias, string email)
        {
            string confirmationId = GetConfirmationId();
            repository.RegisterConfirmationSent(alias, confirmationId);
            mailSender.SendMail(email, mailComposer.ComposeConfirmationMail(alias, confirmationId));
        }

        private string GetConfirmationId()
        {
            return confirmationIdGenerator.NewConfirmationId(); 
        }


        public ConfirmationServices(IRepository repository, IMailSender mailSender, IMailComposer mailComposer, IIdGenerator confirmationIdGenerator)
        {
            this.repository = repository;
            this.confirmationIdGenerator = confirmationIdGenerator;
            this.mailComposer = mailComposer;
            this.mailSender = mailSender;
        }

        public void ConfirmUser(string confirmationId)
        {
            IUser user = repository.GetUserPendingForConfirmation(confirmationId);
            if (user != null)
            {
                user.SetUserAccountStatus(AccountStatus.existAndActive);
                user.CommitChanges();
                repository.RegisterConfirmationDone(confirmationId);
            }
        }
    }
}